

There are 4 HX711 subs here :-

    Thrust_Meas.I    Called by MBasic when the DOUT pin 12 of the HX711 goes -ve, signalling a new                             measurement ready. Set up with "setpin D_OUT%,intl,Thrust_Meas.I".
                     See the sub for a description of the exponential filter formula used.
                     The value of "Exp!" I use is 0.3, but can be changed as a tradeoff between
                     settling time and jitter.
                     Note that the interrupt is inhibited during reading DOUT.

    Meas_Val         Confirms that the DOUT pin is indeed LOW, not just a noise spike.
                     Calls "Get_a_Meas".

    Get_a_Meas       Fetches a measurement from the HX711 chip. DOUT's level is OR'd into the
                     result variable "Data_24%" using the local mask "gam_bit%" if DOUT is HIGH.
                     It is then shifted right for the next bit, 24 bits in total.
                     I am using the HX711's Channel A at x64 Gain, so there are 3 further clocks to
                     set this for the next measurement.

    Zero_Load_Cell   The HX711 will give a count even if there is no load on the cell.
                     Read this value a number of times, average, and store for subtracting from a loaded
                     reading for the load value calculation. Use a known weight to calculate the
                     calibration coefficient of your particular cell. (Counts per gram or ounce etc.)
                     My cell has a cal. coeff. of about 107.8 counts/g, with a no-load reading of
                     around 7500 counts.



sub Thrust_Meas.I
'   -------------
'   HX711 Load Cell Measurement Interrupt.
'   After Initialisation, this subroutine is called about every 100ms to
'   re-measure the Load Cell, with the value Exponentially filtered.
'   The Variable "Thrst_Val!" is then directly updated.
'   The filter calculates a new, smoothed value "y(n)" by using the last
'   smoothed value "y(n-1)" and a new measurement "x(n)", using the exponent
'   weighing value  "w" :-
'           y(n) = w*x(n) + (1-w)*y(n-1)
'   The complete measurement time is about 35ms. ( At 48Mhz.)
'   This routine is called by the HX711 when D_OUT% going -ve generates an interrupt.
  
  setpin D_OUT%,off                       ' Disable D_OUT% -ve interrupt during Measure.
  Meas_Val
  setpin D_OUT%,intl,Thrust_Meas.I        ' Re-enable -ve D_OUT% interrupt.
  
  New_Exp_Val%=cint((Exp!*New_Meas%)+((1-Exp!)*Old_Exp_Val%))'Calc new value,
  Old_Exp_Val%=New_Exp_Val%               ' and update the Old value.
  
  Thrst_Val!=cint((New_Exp_Val%-Load_Cell_Zero%)/Load_Cell_Cal!)  'Scale the result with the Cal. value.
  Thrust_Meas.Fl%=1                       ' Set the flag to signal a Thrust measurement.
end sub
  
  
sub Meas_Val
'   --------
'   Takes a measurement from the Load Cell, and stores it in "New_Meas%".
'   The DOUT pin must confirm a measurement ready with 2 consecutive tests
'   of a LOW level 5ms apart, before a measurement is read.
  
  do                                      ' Double assessment of D_OUT pin for a LOW,
    do
    loop until Pin(D_OUT%)=0
    pause 5                               ' with a short 5ms. pause between.
  loop until Pin(D_OUT%)=0
  
  Get_a_Meas                              ' Read a measurement count,
  New_Meas%=Data_24%                      ' and store.
end sub
  
  
sub Get_a_Meas
'   ----------
'   Fetches a measurement from the HX711 module.
'   The measured value is passed back in the global "Data_24%".

  local gam_cnt%,gam_bit%
  Data_24%=0                              ' &H800000=&B100000000000000000000000,
  gam_bit%=&H800000                       ' 24 bits to build the measurement value.
  For gam_cnt%=23 To 0 Step -1            ' Read 24 bits of data from DOUT,
    PULSE PD_SCK%,0.01                    ' m.s.b. first. 10us. pulse on PD_SCK.
    If Pin(D_OUT%) Then
      Data_24%=Data_24% Or gam_bit%
    EndIf
    gam_bit%=gam_bit%>>1
  Next
  
  PULSE PD_SCK%,0.01                      ' Clock 3 more times for Channel A, x64 Gain,
  PULSE PD_SCK%,0.01                      ' on the NEXT measurement.
  PULSE PD_SCK%,0.01                      ' This sequence has approx. 40us period.
end sub
  
  
sub Zero_Load_Cell
'   --------------
'   Zero the Load Cell, storing the "zero" count in "Load_Cell_Zero%".
'   Ten filtered measures from "New_Exp_Val%" are accumulated and averaged for
'   the Zero value. The Load Cell Measurement Interrupt has been running during
'   the Comms Check, so should be giving reasonably stable results by now.
  
  Thrust_Meas.Fl%=0                       ' Clear the flag and variables.
  Cnt%=0
  Tmp_Int%=0
  do                                      ' Start the measurement loop for 10 results.
    do:loop until Thrust_Meas.Fl%=1       ' Wait until a new measurement has been made,
    Thrust_Meas.Fl%=0                     ' clear the flag,
    Tmp_Int%=Tmp_Int%+New_Exp_Val%        ' and accumulate the result.
    Cnt%= Cnt%+1                          ' Increment the measurement count,
  loop until Cnt%>=10                     ' and test for 10 measures.
  Load_Cell_Zero%=Tmp_Int%/10             ' Average the Zero result and store.
end sub
  
  
